package com.v1.video.db.service;

import java.util.List;

import com.v1.video.AppContext;
import com.v1.video.db.dao.MyThemeTB;
import com.v1.video.db.dao.MyThemeTBDao;
import com.v1.video.db.dao.MyThemeVideoTB;
import com.v1.video.db.dao.MyThemeVideoTBDao;
import com.v1.video.dbcore.greenrobot.dao.query.QueryBuilder;

public class ThemeMyThemeDB {

	private static ThemeMyThemeDB mInstance;
	private MyThemeTBDao mThemeMyTBDao;
	private MyThemeVideoTBDao mThemeMyVideoTBDao;
	private ThemeMyThemeDB() {
		mThemeMyTBDao = AppContext.getDaoSession().getMyThemeTBDao();
		mThemeMyVideoTBDao = AppContext.getDaoSession().getMyThemeVideoTBDao();
	}

	public static ThemeMyThemeDB getInstance() {
		if (mInstance == null) {
			mInstance = new ThemeMyThemeDB();
		}
		return mInstance;
	}
	
	public void insertOrReplaceMyTheme(MyThemeTB entities) {
		try {
//			Collections.reverse(entities);
			mThemeMyTBDao.insertOrReplaceInTx(entities);
		} catch (Exception ex) {

		}
	}
	
	public void insertOrReplaceMyTheme(List<MyThemeTB> entities) {
		try {
//			Collections.reverse(entities);
			mThemeMyTBDao.insertOrReplaceInTx(entities);
		} catch (Exception ex) {

		}
	}
	
	public void insertOrReplaceMyThemeVideo(List<MyThemeVideoTB> entities) {
		try {
//			Collections.reverse(entities);
			mThemeMyVideoTBDao.insertOrReplaceInTx(entities);
		} catch (Exception ex) {

		}
	}
	
	public void insertMyThemeVideo(List<MyThemeVideoTB> entities) {
		try {
//			Collections.reverse(entities);
			mThemeMyVideoTBDao.insertInTx(entities);
			
		} catch (Exception ex) {
           ex.printStackTrace();
		}
	}
	
//	public void insertOrReplaceMyThemeVideo(List<ThemeVideoTB> entities) {
//		try {
////			Collections.reverse(entities);
//			
//			mThemeMyVideoTBDao.insertOrReplaceInTx(entities);
//		} catch (Exception ex) {
//
//		}
//	}
	
	public List<MyThemeTB> getAllMyThemeData() {
		QueryBuilder<MyThemeTB> query = mThemeMyTBDao.queryBuilder();
		return query.build().list();
	}
	
	public List<MyThemeVideoTB> getAllMyThemeVideoData() {
		QueryBuilder<MyThemeVideoTB> query = mThemeMyVideoTBDao.queryBuilder();
		return query.build().list();
	}
	
	public MyThemeTB getSingMyThemeData(String themeId){
		QueryBuilder<MyThemeTB> query = mThemeMyTBDao.queryBuilder()
				.where(MyThemeTBDao.Properties.ThemeID.eq(themeId));
		return query.unique();
	}
	
	public List<MyThemeVideoTB> getMyThemdVideoFromMyTheme(String themeId){
		QueryBuilder<MyThemeVideoTB> query = mThemeMyVideoTBDao.queryBuilder()
				.where(MyThemeVideoTBDao.Properties.ThemeID.eq(themeId));
		return query.build().list();
	}
	
	public boolean deleteTheTheme(String themeId){
		boolean bool = false;
		
		return bool;
	}
	
	public void delete(MyThemeTB entity) {
		mThemeMyTBDao.delete(entity);
	}
	
	public void deleteInTx(List<MyThemeVideoTB> entities) {
		mThemeMyVideoTBDao.deleteInTx(entities);
	}
	
//	public List<MyThemeTB> getSingleData(int page, int rows) {
//		QueryBuilder<MyThemeTB> query = mThemeMyTBDao.queryBuilder()
//				.orderDesc(MyThemeTBDao.Properties.Id).limit(rows)
//				.offset(page * rows);
//		return query.build().list();
//	}
	
//	public List<MyThemeTB> getSingleQuery(String cid, int page, int rows) {
//		List<MyThemeTB> videoList= new ArrayList<MyThemeTB>();
//		/* String sql = "select v." + MyThemeTBDao.Properties.Id.columnName + ",v."
//				+ MyThemeTBDao.Properties.ThemeID.columnName + ",v."
//				+ MyThemeTBDao.Properties.ThemeName.columnName + ",v."
//				+ NewsTBDao.Properties.Title.columnName + ",v."
//				+ NewsTBDao.Properties.Pic.columnName + ",o."
//				+ OperateTBDao.Properties.Flag.columnName + ",v."
//				+ NewsTBDao.Properties.Name.columnName +",v."
//				+ VideoTBDao.Properties.VideoUrl.columnName +",v."
//				+ VideoTBDao.Properties.At.columnName+ " from "
//				+ NewsTBDao.TABLENAME + " as v left join "
//				+ OperateTBDao.TABLENAME + " as o on v."
//				+ NewsTBDao.Properties.Vid.columnName + "= o."
//				+ OperateTBDao.Properties.Vid.columnName + " and v."
//				+ NewsTBDao.Properties.Cid.columnName + " =o."
//				+ OperateTBDao.Properties.Cid.columnName + " where v."
//				+ NewsTBDao.Properties.Cid.columnName + "='" + cid
//				+ "' order by v." + NewsTBDao.Properties.Id.columnName
//				+ " desc limit " + page * rows + "," + rows;
//		Cursor cursor = AppContext.getDaoMaster().getDatabase().rawQuery(sql, null);
//		List<MyThemeTB> VideoLst = null;
//		try {
//			if (cursor != null && cursor.getCount() > 0) {
//				cursor.moveToFirst();
//				VideoLst = new ArrayList<MyThemeTB>();
//				do {
//					NewsTB video = new NewsTB();
//					video.setId(cursor.getLong(cursor
//							.getColumnIndex(VideoTBDao.Properties.Id.columnName)));
//					video.setVid(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Vid.columnName)));
//					video.setCid(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Cid.columnName)));
//					video.setTitle(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Title.columnName)));
//					video.setPic(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Pic.columnName)));
//					video.setDuration(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Duration.columnName)));
//					video.setFlag(cursor.getString(cursor
//							.getColumnIndex(OperateTBDao.Properties.Flag.columnName)));
//					video.setName(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.Name.columnName)));
//					video.setVideoUrl(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.VideoUrl.columnName)));
//					video.setAt(cursor.getString(cursor
//							.getColumnIndex(VideoTBDao.Properties.At.columnName)));
//					VideoLst.add(video);
//				} while (cursor.moveToNext());
//			}
//		} finally {
//			cursor.close();
//		} return VideoLst */
//		return videoList;
//	}
}
